﻿using CommonType;
using MediatR;
using S_Gridding.Domains.FoundGridding;
using S_Gridding.Infrastructure.Interface.IRead;
using S_Gridding.Read.Api.Application.Commands;
using S_Userinfor.Domains;

namespace S_Gridding.Read.Api.Application.CommandsHanders
{
    public class TeamigridShowCommandsHanders : IRequestHandler<TeamigridShowCommands, PageResule<Teamgrid>>
    {
        private readonly IDTeamgRepository _dTeamg;

        public TeamigridShowCommandsHanders(IDTeamgRepository dTeamg)
        {
            _dTeamg = dTeamg;
        }

        public async Task<PageResule<Teamgrid>> Handle(TeamigridShowCommands request, CancellationToken cancellationToken)
        {
            var query = $" SELECT * FROM Teamgrid WHERE 1=1 AND IsDel !=TRUE ";

           
            var tacount = await _dTeamg.SelectAsync(request.PageSize, query);

            query += $"  ORDER BY ID DESC  LIMIT {(request.PageIndex - 1) * request.PageSize},@PageSize;";

            var List = await _dTeamg.SelectAsync(request.PageSize, query);

            return new PageResule<Teamgrid>
            {
                TotalCount = tacount.Count(),
                PageCount = (int)Math.Ceiling((List.Count() * 1.0) / request.PageSize),
                Data = List,
            };
        }
    }
}
